package com.doctor.web.service.impl;

import com.doctor.common.core.domain.entity.SysUser;
import com.doctor.common.utils.SecurityUtils;
import com.doctor.system.mapper.SysUserMapper;
import com.doctor.web.domain.OrderDO;
import com.doctor.web.domain.ProductDO;
import com.doctor.web.mapper.OrderMapper;
import com.doctor.web.mapper.ProductMapper;
import com.doctor.web.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private SysUserMapper userMapper;

    @Override
    public void payOrder(OrderDO orderDO) {
        Integer quantity = orderDO.getQuantity();

        OrderDO orderById = orderMapper.getById(orderDO.getId());
        Long userId = orderById.getUserId();
        SysUser sysUser = userMapper.selectUserById(userId);
        Long productId = orderById.getProductId();
        ProductDO productDO = productMapper.getById(productId);
        BigDecimal price = productDO.getPrice();
        //获取总金额
        BigDecimal quantityBigDecimal = new BigDecimal(quantity);
        BigDecimal total = price.multiply(quantityBigDecimal);
        orderById.setUnitPrice(price);
        orderById.setTotalPrice(total);
        orderById.setOrderStatus(1);
        orderById.setQuantity(quantity);
        orderMapper.updateById(orderById);

        //product计数：销量、库存
        productDO.setSalesNumber(productDO.getSalesNumber()+1);
        productDO.setStock(productDO.getStock()-1);
        productMapper.updateById(productDO);
        //user计数：余额
        BigDecimal money = sysUser.getMoney().subtract(total);
        sysUser.setMoney(money);
        userMapper.updateUser(sysUser);
    }

    @Override
    public Long save(Long productId) {
        ProductDO productDO = productMapper.getById(productId);
        OrderDO orderDO = new OrderDO();
        orderDO.setProductId(productId);
        orderDO.setUserId(SecurityUtils.getUserId());
        orderDO.setCreateTime(new Date());
        orderDO.setCreateBy(SecurityUtils.getUserId().toString());
        orderDO.setImgUrl(productDO.getImgUrl());
        orderDO.setOrderStatus(0);
        orderMapper.save(orderDO);

        Long id = orderDO.getId();

        return id;
    }

    @Override
    public boolean removeById(Long id) {
        return orderMapper.removeById(id) > 0;
    }

    @Override
    public boolean updateById(OrderDO orderDO) {
        return orderMapper.updateById(orderDO) > 0;
    }

    @Override
    public OrderDO getById(Long id) {
        return orderMapper.getById(id);
    }

    @Override
    public List<OrderDO> list() {
        return orderMapper.list();
    }

    @Override
    public List<OrderDO> getOrdersByUserId(Long userId) {
        return orderMapper.getOrdersByUserId(userId);
    }

    @Override
    public List<OrderDO> getOrdersByUserIdAndStatus(Long userId, Integer orderStatus) {
        return orderMapper.getOrdersByUserIdAndStatus(userId, orderStatus);
    }

    @Override
    public boolean updateOrderStatus(Long orderId, Integer orderStatus) {
        int rows = orderMapper.updateOrderStatus(orderId, orderStatus);
        return rows > 0;
    }
}